﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E_Shop.Common;
using ESHOP.DAO;
using ESHOP.Domain;
using ESHOP.UTIL;

namespace E_Shop
{
    public partial class CompanyPostNewProduct : Authentication
    {
        private DanhMucDAO _danhMucDao = new DanhMucDAO();
        private QuocGiaDAO _quocgiadao = new QuocGiaDAO();
        public SANPHAM _sanpham = new SANPHAM();
        private readonly SanPhamDAO _sanphamdao = new SanPhamDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDuLieu();
            }
        }

        private void LoadDuLieu()
        {
            CommonFunc.BindDropDownList(ddlcategory, _danhMucDao.GetList(), "TENDM", "ID", true);
            CommonFunc.BindDropDownList(ddlcounttry, _quocgiadao.GetList(), "TENQUOCGIA", "ID", true);

        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
           

            Control myControl1 = FindControl("choseimage");
            string savePath = Server.MapPath("~/Images/Product/" + fuImage.FileName);
            if (fuImage.HasFile)
            {
                
                fuImage.SaveAs(savePath);

            }
            _sanpham.HINHANH = fuImage.FileName;
            _sanpham.ID = _sanphamdao.NewId();
             
            _sanpham.MADANHMUC = ddlcategory.SelectedValue;
            _sanpham.MAQUOCGIA = ddlcounttry.SelectedValue;
            _sanpham.GIA = decimal.Parse(txtprice.Text.Trim().ToString());
            _sanpham.TENSANPHAM = txtProduct.Text.Trim();
            _sanpham.CHITIET= txtmota.Text.Trim();
            _sanpham.MACONGTY = Acount.MACT;
            _sanphamdao.Insert(_sanpham);
            String webmanage = WebUrl.MANAGEPRODUCT.ToString();
            String addnewproduct = WebUrl.ADDNEWPRODUCT.ToString();
            String key = String.Format("{0}&manage={1}&addnew={2}&name={3}", WebUrl.REGISTER_PRODUCT, webmanage, addnewproduct, "Product");
            Response.Redirect(key, true);
        }

    }
}